//
//  IAAAuthCheckModel.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/5.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAAuthCheckModel.h"

@implementation IAAAuthCheckTaskItem
@end
@implementation IAAAuthCheckModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"taskList" : [IAAAuthCheckTaskItem class]};
}

- (void)addMoreWithData:(IAAAuthCheckModel *)model {
    [self.taskList addObjectsFromArray:model.taskList];
}
@end



@implementation IAAAuthCheckImgItem
- (UIImage *)imageObject {
    if (self.imageBase64.length) {
        NSData *data = [[NSData alloc] initWithBase64EncodedString:self.imageBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
        return [UIImage imageWithData:data];
    }
    return nil;
}
@end
@implementation IAAAuthCheckSortItem
- (instancetype)init {
    self = [super init];
    if (self) {
        _imgArray = [NSMutableArray array];
    }
    return self;
}
- (BOOL)haveImageType:(NSInteger)imageType {
    for (IAAAuthCheckImgItem * item in self.imgArray) {
        if (item.imageType == imageType) {
            return YES;
        }
    }
    return NO;
}
@end
@implementation IAAAuthCheckDetailModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"imgelist" : [IAAAuthCheckImgItem class]};
}

- (NSInteger)canAddNum {
    NSInteger hasAddNum = 0;
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.isAdd) {
            ++hasAddNum;
        }
    }
    return self.maxReTakePictureNum - hasAddNum;
}
- (NSData *)imageDataWithIndex:(NSInteger)index {
    if (index < self.imgelist.count) {
        IAAAuthCheckImgItem *item = self.imgelist[index];
        if (item.imageBase64.length) {
            return [[NSData alloc] initWithBase64EncodedString:item.imageBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
        }
    }
    
    return nil;
}
- (IAAAuthCheckImgItem *)forceReTakePicture {
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.isForce && item.needUpload) {
            return item;
        }
    }
    return nil;
}
- (BOOL)isNeedUploadPicture {
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if ((item.isAdd || item.isReplace) && item.needUpload) {
            return YES;
        }
    }
    return NO;
}
- (void)configNeedUploadFlag:(BOOL)flag {
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.isAdd || item.isReplace) {
            item.needUpload = flag;
        }
    }
}
- (void)toSortImageList {
    NSMutableArray<IAAAuthCheckSortItem *> *mArray = [NSMutableArray arrayWithCapacity:self.imgelist.count];
    if ([self isNeedSortImgList]) {
        NSLog(@"---需要做组合");
        // 整理组合的数组
        IAAAuthCheckImgItem *item16 = [self getImageType:16];
        IAAAuthCheckImgItem *item28 = [self getImageType:28];
        IAAAuthCheckImgItem *item29 = [self getImageType:29];
        IAAAuthCheckImgItem *item13 = [self getImageType:13];
        IAAAuthCheckImgItem *item14 = [self getImageType:14];
        IAAAuthCheckSortItem *sortItem1 = [[IAAAuthCheckSortItem alloc] init];
        IAAAuthCheckSortItem *sortItem2 = [[IAAAuthCheckSortItem alloc] init];
        [sortItem1.imgArray addObject:item16];
        [sortItem2.imgArray addObject:item16];
        // 组合1 16 28 16 29
        if (item28) {
            [sortItem1.imgArray addObject:item28];
            if (item29) {
                [sortItem1.imgArray addObject:item16];
            }
        }
        if (item29) {
            [sortItem1.imgArray addObject:item29];
        }
        // 组合2 16 13 16 14
        if (item13) {
            [sortItem2.imgArray addObject:item13];
            if (item14) {
                [sortItem2.imgArray addObject:item16];
            }
        }
        if (item14) {
            [sortItem2.imgArray addObject:item14];
        }
        // 整合
        if (sortItem1.imgArray.count > 1) {
            [mArray addObject:sortItem1];
        }
        if (sortItem2.imgArray.count > 1) {
            [mArray addObject:sortItem2];
        }
        // 把非组合的先整理出来
        for (IAAAuthCheckImgItem *item in self.imgelist) {
            if ((item.imageType != 16)
                && (item.imageType != 28)
                && (item.imageType != 29)
                && (item.imageType != 13)
                && (item.imageType != 14)) {
                IAAAuthCheckSortItem *sortItem = [[IAAAuthCheckSortItem alloc] init];
                [sortItem.imgArray addObject:item];
                [mArray addObject:sortItem];
            }
        }
    } else {
        NSLog(@"不需要做组合");
        for (IAAAuthCheckImgItem *item in self.imgelist) {
            IAAAuthCheckSortItem *sortItem = [[IAAAuthCheckSortItem alloc] init];
            [sortItem.imgArray addObject:item];
            [mArray addObject:sortItem];
        }
    }
    self.sortImgList = mArray;
}
- (IAAAuthCheckImgItem *)getImageType:(NSInteger)imageType {
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.imageType == imageType) {
            return item;
        }
    }
    return nil;
}
// 先判断28
//
// 16 28
// 16 29
//
// 16 13
// 16 14
- (BOOL)isNeedSortImgList {
    BOOL have16 = NO;
    BOOL haveXX = NO;
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.imageType == 16) {
            have16 = YES;
            break;
        }
    }
    for (IAAAuthCheckImgItem *item in self.imgelist) {
        if (item.imageType == 28
            || item.imageType == 29
            || item.imageType == 13
            || item.imageType == 14) {
            haveXX = YES;
            break;
        }
    }
    
    return have16 && haveXX;
}
- (void)changeFalseData {
    self.imgelist[0].imageType = 16;
    self.imgelist[1].imageType = 28;
    self.imgelist[1].isReTakePicture = YES;
    self.imgelist[2].imageType = 29;
    self.imgelist[3].imageType = 13;
    self.imgelist[4].imageType = 14;
}
@end




@implementation IAAAuChUpPicItem
- (instancetype)initWithAuChImgItem:(IAAAuthCheckImgItem *)item {
    self = [super init];
    if (self) {
        _fileFormat = [item.fileFormat copy];
        _fileType = item.imageType;
        _fileSize = item.fileSize;
        _opType = (item.ufid.length == 0) ? 0 : 1;   //!< 目前还没有删除功能
//        _ufid = item.isAdd ? @"" : item.ufid;
        _ufid = item.ufid;
        _fileContent = [item.imageBase64 copy];
    }
    return self;
}
@end
@implementation IAAAuChUploadPicModel
- (instancetype)initWithBusiId:(NSString *)busiId token:(NSString *)token AuChDetailModel:(IAAAuthCheckDetailModel *)model {
    self = [super init];
    if (self) {
        _acceptId = [busiId copy];
        _token = [token copy];
        _osName = @"iOS";
        _contentId = @"";
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:model.imgelist.count];
        for (IAAAuthCheckImgItem *Img in model.imgelist) {
            if (Img.isAdd || Img.isReplace) {
                IAAAuChUpPicItem *upItem = [[IAAAuChUpPicItem alloc] initWithAuChImgItem:Img];
                [mArray addObject:upItem];
            }
        }
        _foList = mArray;
    }
    return self;
}
@end



@implementation IAAAuthCheckCommitModel
- (instancetype)initWithAuChTaskItem:(IAAAuthCheckTaskItem *)item {
    self = [super init];
    if (self) {
        _taskId = item.taskId;
        _busiId = [item.busiId copy];
        _taskLevel = item.taskLevel;
        _osName = @"iOS";
    }
    return self;
}
@end



@implementation IAAAuChPhoneCodeModel
@end
@implementation IAAAuthCheckCheckerItem
@end
@implementation IAAAuthCheckCheckerModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"checkerList" : [IAAAuthCheckCheckerItem class]};
}

- (void)addMoreWithData:(IAAAuthCheckCheckerModel *)model {
    [self.checkerList addObjectsFromArray:model.checkerList];
}
@end

